package models;

import java.awt.Graphics2D;
import java.awt.Image;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import javax.swing.ImageIcon;

import models.tent.Tent;

public class Sprite {
	
	private int posX, posY;
	private int width, height;
	private int frame;
	private int recFrame;
	private int nFramesRun, nFramesAttack, nFramesDeath;
	private int currentlyStatus;
	private boolean finalFrameDeath = false;
	
	private Image[] sprites = new Image[20];
	

	public Sprite(){
		recFrame = 0;		
	}

	public void addFrame(String path, int state, int number) throws IOException{
		
		Image image = new ImageIcon(getClass().getResource(path)).getImage();
		image = image.getScaledInstance(width, width, Image.SCALE_AREA_AVERAGING);
		
		sprites[number] = image;
		//sprites.add(image);
		if(state == 0){
			nFramesRun++;
		}else if(state == 1){
			nFramesAttack++;
		}else if(state == 2){
			nFramesDeath++;
		}
		
	}
	
	public boolean collide(Sprite sp) {
		int w1,w2,x1,y1,x2,y2;

		w1=getWidth();		// width of sprite1
		w2=sp.getWidth();	// width of sprite2
		x1=getPosX();		// pos. X of sprite1
		y1=getPosY();		// pos. Y of sprite1
		x2=sp.getPosX();	// pos. X of sprite2
		y2=sp.getPosY();	// pos. Y of sprite2

		if (((x1+w1/2)>x2)&&((x2+w2/2)>x1)&&(y1==y2)) {
			return true;
		} else {
			return false;
		}
	}
	
	public boolean collideWithTent(Tent t, int speed){
		int x1,y1,x2,y2;

		x1=getPosX();		// pos. X of sprite1
		y1=getPosY();		// pos. Y of sprite1
		x2=t.getPosX();		// pos. X of tent
		y2=t.getPosY();		// pos. Y of tent

		if ((x1==x2)&&(y1==y2)||(x1<x2)&&(x1+speed)>x2&&(y1==y2)) {
			return true;
		} else {
			return false;
		}
	}

	public void draw(Graphics2D g){
		
		g.drawImage(sprites[frame], posX, posY, null);
		//g.drawImage(sprites.get(frame), posX, posY, null);
				
	}	
	
	public void update(){
		recFrame++;
		if(currentlyStatus == 0){
			if(recFrame > 15){
				if(frame < nFramesRun){
					frame = ++frame % nFramesRun;
				}	
				recFrame = 0;		
			}
		}else if(currentlyStatus == 1){
			if(recFrame > 15){
				if(frame < nFramesRun+nFramesAttack){
					if(frame == nFramesRun+nFramesAttack-1){
						frame = nFramesRun;
					}else{
						frame = ++frame % nFramesRun+nFramesAttack;
					}
				}	
				recFrame = 0;		
			}
		}else if(currentlyStatus == 2){
			if(recFrame > 25 && !finalFrameDeath){
				if(frame < sprites.length){
					frame = ++frame % nFramesRun+nFramesAttack+nFramesDeath;
					if(frame == 8){
						finalFrameDeath = true;
					}
				}	
				recFrame = 0;		
			}
		}
	}


	public int getWidth() {
		return width;
	}

	public int getHeight() {
		return height;
	}
	
	public void setWidth(int width) {
		this.width = width;
	}

	public void setHeight(int height) {
		this.height = height;
	}
	
	public void setFrame(int frame){
		this.frame = frame;
	}
	
	public int getFrame(){
		return frame;
	}

	public int getPosX() {
		return posX;
	}

	public void setPosX(int posX) {
		this.posX = posX;
	}

	public int getPosY() {
		return posY;
	}

	public void setPosY(int posY) {
		this.posY = posY;
	}

	public int getCurrentlyStatus() {
		return currentlyStatus;
	}

	public void setCurrentlyStatus(int status) {
		currentlyStatus = status;
		if(currentlyStatus == 0){
			frame = 0;
		}else if(currentlyStatus == 1){
			frame = nFramesRun;
		}else if(currentlyStatus == 2){
			frame = nFramesRun + nFramesAttack;
		}	
		recFrame = 0;
	}	
}
